package com.iemes.controller.quality;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DepartmentForMap;
import com.iemes.entity.PrecautionaryMeasuresFormMap;
import com.iemes.mapper.quality.PrecautionaryMeasuresMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;

@RequestMapping("/quality")
@Controller
public class PrecautionaryMeasuresController extends BaseController {

	@Inject
	private PrecautionaryMeasuresMapper precautionaryMeasuresMapper;

	
	@RequestMapping("precautionarymeasures")
	private String fine(Model model){
		model.addAttribute("res", findByRes());
		//获取部门名称
		List<DepartmentForMap> departmentForMaps=baseMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("departmentForMaps", departmentForMaps);
		return Common.BACKGROUND_PATH + "/quality/precautionarymeasures/precautionarymeasures_list";
	}
	
	@ResponseBody
	@RequestMapping("precautionarymeasures_findByPage")
	public PageView findByPage(String pageNow, String pageSize) throws Exception 
	{
		PrecautionaryMeasuresFormMap precautionaryMeasuresFormMap=getFormMap(PrecautionaryMeasuresFormMap.class);
		List<Map<String,Object>> precautionaryMeasuresFormMaps=precautionaryMeasuresMapper.getPrecautionaryMeasuresByCondition(precautionaryMeasuresFormMap);
		LuogicPaging(pageNow, pageSize, precautionaryMeasuresFormMaps);
		return pageView;
	}
		
	@ResponseBody
	@RequestMapping(value="precautionarymeasures_deleteEntity")
	@SystemLog(module="纠正预防",methods="纠正预防-删除")//凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		PrecautionaryMeasuresFormMap precautionaryMeasuresFormMap=getFormMap(PrecautionaryMeasuresFormMap.class);
		String[] ids = getParaValues("ids");
		String arr=ids[0];
		String[] str=arr.split(",");
		for(int i=0;i<str.length;i++){
			precautionaryMeasuresFormMap.put("state", 1);
			precautionaryMeasuresFormMap.put("id", str[i]);
			baseMapper.editEntity(precautionaryMeasuresFormMap);
		}
		return "success";
	}
	
	
	@RequestMapping("precautionarymeasures_addUI")
	public String addUI(Model model) throws Exception {
		//获取部门名称
		List<DepartmentForMap> departmentForMaps=baseMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("departmentForMaps", departmentForMaps);
		return Common.BACKGROUND_PATH + "/quality/precautionarymeasures/precautionarymeasures_add";
	}
	
	
	@ResponseBody
	@RequestMapping("precautionarymeasures_addEntity")
//	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module="纠正预防",methods="纠正预防-新增")//凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		PrecautionaryMeasuresFormMap precautionaryMeasuresFormMap=getFormMap(PrecautionaryMeasuresFormMap.class);
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		precautionaryMeasuresFormMap.put("raiseDate", sdf.format(date));
		precautionaryMeasuresFormMap.put("endTime", sdf.format(date));
		precautionaryMeasuresFormMap.put("state", 0);
		baseMapper.addEntity(precautionaryMeasuresFormMap);
		return "success";
	}
	
	
	@RequestMapping("precautionarymeasures_edit")
	public String editUI(Model model) throws Exception 
	{
		String id = getPara("id");
		PrecautionaryMeasuresFormMap precautionaryMeasuresFormMap=precautionaryMeasuresMapper.precautionaryMeasuresFormMapById(Integer.parseInt(id));
		model.addAttribute("precautionaryMeasuresFormMap", precautionaryMeasuresFormMap);
		
		//获取部门信息
		List<DepartmentForMap> departmentForMaps=baseMapper.findByWhere(new DepartmentForMap());
		model.addAttribute("departmentForMaps", departmentForMaps);
		
		return Common.BACKGROUND_PATH + "/quality/precautionarymeasures/precautionarymeasures_edit";
	}
	
	
	@ResponseBody
	@RequestMapping(value="precautionarymeasures_editEntity")
	@SystemLog(module="纠正预防",methods="纠正预防-修改")//凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		PrecautionaryMeasuresFormMap precautionaryMeasuresFormMap=getFormMap(PrecautionaryMeasuresFormMap.class);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		precautionaryMeasuresFormMap.put("endTime", sdf.format(date));
		baseMapper.editEntity(precautionaryMeasuresFormMap);
		return "success";
	}
	
}
